using System.Collections.Generic;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Skill;
using Gameplay.PVE.Utils;
using Yoozoo.Managers;

namespace Gameplay.PVE
{
    public class ObstacleBattleComponent : IComponent, ITransmit
    {
        private UnitBase unit;
        
        private Dictionary<int,SkillBase> skills = new Dictionary<int,SkillBase>();
        private SkillPassiveTrigger passiveTrigger;
        private ElementSystem elementSystem;

        private void InitSkills()
        {
            skills.Clear();
            unit.Data.skillList.Clear();
            for (int i = 0; i < unit.Data.obstacleConfig.skills.Count; i++)
            {
                unit.Data.skillList.Add(unit.Data.obstacleConfig.skills[i]);
            }
            for (int i = 0; i < unit.Data.skillList.Count; i++)
            {
                var config = PveUtils.GetSkillConfig(unit.Data.skillList[i]);
                SkillBase skill = null;
                if (config.type == 6)
                {
                    skill = ClassManager.Get<SkillPassive>();
                    skill.Initialize(unit,unit.Data.skillList[i],config);
                    passiveTrigger.AddSkillPassive(unit.Data.skillList[i],skill);
                }
                
                if (skill == null)
                {
                    continue;
                }
                skill.Initialize(unit,unit.Data.skillList[i],config);
                skill.skillType = 2;
                skills.Add(unit.Data.skillList[i],skill);
            }
        }
        
        public void OnReset()
        {
            foreach (var item in skills.Values)
            {
                ClassManager.Free(item);
            }
            skills.Clear();
        }

        public void OnInitialize(UnitBase unit)
        {
            this.unit = unit;
            elementSystem = ClassManager.Get<ElementSystem>();
            elementSystem.Initialize();
            passiveTrigger = ClassManager.Get<SkillPassiveTrigger>();
            passiveTrigger.Initialize(unit,elementSystem);
        }

        public void OnBirth()
        {
            InitSkills();
        }

        public void OnDead()
        {
            
        }

        public void OnUpdate()
        {
            foreach (var item in skills.Values)
            {
                item.Update();
            }
            CheckPassiveSkill();
            elementSystem.Update();
        }
        
        private void CheckPassiveSkill()
        {
            if (unit.Data.isDead)
            {
                return;
            }
            passiveTrigger.Update();
        }

        public void OnTransmit(ETransmitType type, BaseTransmitArg arg)
        {
            if (type == ETransmitType.AddElement)
            {
                var elementArg = arg as AddElementArg;
                elementSystem.AddElement(-1, elementArg.target,elementArg.source,elementArg.elementConfig,elementArg.bulletPosition,elementArg.isCrit,elementArg.rigidBreak,elementArg.sourceSkill,elementArg.isSkillAdditive);
            }
            else if (type == ETransmitType.ChangePassiveSkill)
            {
                var changePassiveSkillArg = arg as ChangePassiveSkill;
                int skillId = changePassiveSkillArg.skillId;
                if (changePassiveSkillArg.state == 0)
                {
                    if (!skills.ContainsKey(skillId))
                    {
                        var config = PveUtils.GetSkillConfig(skillId);
                        SkillBase skill = null;
                        if (config.type == 2)
                        {
                            skill = ClassManager.Get<SkillAuto>();
                        }
                        else if (config.type == 6)
                        {
                            skill = ClassManager.Get<SkillPassive>();
                        }

                        if (skill != null)
                        {
                            unit.Data.skillList.Add(skillId);
                            skill.Initialize(unit,skillId,config);
                            skill.level = changePassiveSkillArg.skillLevel;
                            skills.Add(skillId,skill);
                            skill.skillType = 2;
                            passiveTrigger.AddSkillPassive(skillId,skill);
                        }
                    }
                }
                /*else if (changePassiveSkillArg.state == 1)
                {
                    //passiveTrigger.RemoveSkillPassive(changePassiveSkillArg.skillId);
                    removePassiveSkillList.Add(changePassiveSkillArg.skillId);
                }*/
            }
            passiveTrigger.Transmit(type, arg);
            
        }
    }
}